#!/usr/bin/perl

use Cwd 'abs_path';

use Getopt::Std;
getopt("i:o:d", \%args); 
my $input_direc = $args{i};
my $output_direc= $args{o};
my $dir= $args{d};
chomp $input_direc;
chomp $output_direc;
chomp $dir;
if($input_direc eq "")
{
	die "entered input directory is empty\n";
	
}
if($output_direc eq "")
{
        die "entered output directory is empty\n";

}
#if($dir eq "")
#{
 #       die "entered working directory is empty\n";

#}

@arr_dir = split("",$input_direc);
$arr_d = @arr_dir;
if($arr_dir[$arr_d-1] eq "/")
{
	$arr_dir[$arr_d-1] = "";
}
$input_direc = join('',@arr_dir);

@arr_dir = split("",$output_direc);
$arr_d = @arr_dir;
if($arr_dir[$arr_d-1] eq "/")
{
        $arr_dir[$arr_d-1] = "";
}
$output_direc = join('',@arr_dir);

#@arr_dir = split("",$dir);
#$arr_d = @arr_dir;
#if($arr_dir[$arr_d-1] eq "/")
#{
#        $arr_dir[$arr_d-1] = "";
#}
#$dir = join('',@arr_dir);

#print "entered path : $dir\n";
$round = sprintf("%.0f", rand()*time());
$rounded = $round."_dirfile";
#system("pwd > $rounded");
#open(DRBUFF,$rounded);
#$line = <DRBUFF>;
#system("rm $rounded");
$line = abs_path($0);
chomp $line;
@DR_array = split('/',$line);
pop(@DR_array);
pop(@DR_array);
$dir = join("/",@DR_array);
#print $input_direc."\n";
#die "entered directory is empty\n";
$dir_inputfile = $round."inputfiles";
system("mkdir $dir/temp");
system("mkdir $dir/temp/$dir_inputfile/");
system("cp $input_direc/* $dir/temp/$dir_inputfile/");


#die "Done with convertion";
#Listing all the CEL file names in to file filename.txt
$filename_ind = $round."filename.txt";
system("ls $dir/temp/$dir_inputfile > $dir/temp/$filename_ind");
#open(BUFF,"<$dir/filename.txt");
open BUFF, "< $dir/temp/$filename_ind" or die "Can't open $dir/temp/$filename_ind : $!";
@output=<BUFF>;
system("rm -rf $dir/temp/$filename_ind");



#Converting each binary cel file to ascii format
$ascii_dir = $round."ascii";
for($i=0;$i<@output;$i++)
{
	#print "converting file " . $output[$i] . "\n";
        if($output[$i] =~ m/CEL/)
        {
                chomp($output[$i]);
		my $cmd = "$dir/bin/apt/bin/apt-cel-convert -f text -o $dir/temp/$ascii_dir  $dir/temp/$dir_inputfile/" . $output[$i];
		#print "$cmd\n";
		system($cmd);
 	}
}
system("rm -rf $dir/temp/$ascii_dir/apt-cel-convert.log");
system("rm -rf $dir/temp/$dir_inputfile");
#die "Done with convertion";



#removing the file filename.txt
#system("rm -rf $dir/filename.txt");
#Listing all the CEL file names in to file filename.txt
system("ls $dir/temp/$ascii_dir > $dir/temp/$filename_ind");
#open(BUFF,"<$dir/filename.txt");
open BUFF, "< $dir/temp/$filename_ind" or die "Can't open $dir/temp/$filename_ind : $!";
@output=<BUFF>;
system("rm -rf $dir/temp/$filename_ind");

$ascii_dir_out =$ascii_dir."out";
system("mkdir $dir/temp/$ascii_dir_out");
#Converting each HTHGU cel file to HGU133A cel file
for($i=0;$i<@output;$i++)
{
    print "About to call script for file " . $output[$i];
     #   if($output[$i] =~ m/CEL/)
      #  {
                chomp($output[$i]);
		#print WRBUFF $output[$i]."\n";
                
	system("/usr/bin/perl  $dir/bin/convert3_hs.pl  ".$output[$i]."  $dir"." $ascii_dir");
       # }
}
system("rm -rf $dir/temp/$ascii_dir");

#system("/usr/local/biotools/r/R-2.11.0/bin/R CMD INSTALL $dir/bin/newonehgu133plus2geneccdscdf.tar.gz");

#die "Done with convertion";
#removing the file filename.txt
#system("rm -rf $dir/filename.txt");
#Listing all the converted CEL file names in to file filename.txt
system("ls $dir/temp/$ascii_dir_out > $dir/temp/filename.txt");
open(BUFF,"<$dir/temp/filename.txt");
#open BUFF, "< $dir/filename.txt" or die "Can't open $dir/filename.txt : $!";
@output=<BUFF>;
system("rm -rf $dir/temp/filename.txt");
print @output."\n";
$ascii_dir_out_1 = $ascii_dir_out."_1";
system("mkdir $dir/temp/$ascii_dir_out_1");
system("mv $dir/temp/$ascii_dir_out $dir/temp/$ascii_dir_out_1");
#FRMA normalization and calculating present calls for each CEL file
#my $dir_r = $dir."/samplefiles_out";
for($i=0;$i<@output;$i++)
{
	if($output[$i] =~ m/CEL/ || $output[$i] =~ m/cel/)
	{
#		chomp($output[$i]);
		system("mv $dir/temp/$ascii_dir_out $dir/temp/1");
		system("cp $dir/temp/$ascii_dir_out_1/$output[$i] $dir/temp/$ascii_dir_out");
		system("source /home/m081429/.bash_profile");
		#print $ascii_dir_out."\n";
		system("/usr/local/biotools/r/R-2.11.0/bin/Rscript $dir/bin/script_conv_frma.R $dir/temp/$ascii_dir_out  $dir/bin/rlib");

#		system("rm -rf $dir/samplefiles_out/$output[$i]");
#	}
#}
#system("rm -rf $dir/filename.txt");
		my $normalize_file;
		my $call_file;
		my $normalize_file_out;
		my $call_file_out;
		$normalize_file = $ascii_dir_out.".normalized";
		$call_file = $normalize_file.".call";
		$normalize_file_out = $normalize_file.".out";
		$call_file_out  = $call_file.".out";
		my $line;
		my %hash;
		open BUFF, "< $dir/bin/HTHGU_probes.txt" or die "Can't open $dir/bin/HTHGU_probes.txt : $!";
		$line = <BUFF>;
		while($line = <BUFF>)
		{
			chomp $line;
			$line =~ s/\n//g;
			$line =~ s/\r//g;
			$hash{$line} = "k";
		}

		open BUFF, "< $dir/temp/$ascii_dir_out/$normalize_file" or die "Can't open $dir/temp/$ascii_dir_out/$normalize_file : $!";
#open(WRBUFF,">$dir/temp/$ascii_dir_out/$normalize_file_out");
		print "$dir/temp/$ascii_dir_out/$normalize_file\n";
		open BUFF1, "< $dir/temp/$ascii_dir_out/$call_file" or die "Can't open $dir/temp/$ascii_dir_out/$call_file : $!";
#open(WRBUFF1,">$dir/temp/$ascii_dir_out/$call_file_out");
		print "$dir/temp/$ascii_dir_out/$call_file\n";
		$line = <BUFF>;
		chomp $line;
		$line =~ s/\n//g;
		$line =~ s/\r//g;
		@cel_names = split("\t",$line);
#shift(@cel_names);
		$len_array = @cel_names;
#print WRBUFF $line;
		$line = <BUFF1>;
#print  WRBUFF1 "\t".$line;
		for($i=0;$i<@cel_names;$i++)
		{
			$name_file  = $cel_names[$i]."";
			$WRBUFF = "WRBUFF".$i;
			open($WRBUFF, ">$output_direc/$name_file");
			print "$output_direc/$name_file\n";
			print $WRBUFF "SEGID"."\t"."EXPR_MEAS"."\t"."CONF"."\t"."CALL"."\n";
	
		}
		while($line = <BUFF>)
		{
			$line1 = <BUFF1>;
        		chomp $line;
        		$line =~ s/\n//g;
        		$line =~ s/\r//g;
	 		chomp $line1;
			$line1 =~ s/\n//g;
			$line1 =~ s/\r//g;
			@array = split("\t",$line);
			@array1 = split("\t",$line1);
        		if($hash{$array[0]} eq "k" && $hash{$array1[0]} eq "k")
			{
		#print WRBUFF $line."\n";
				for($i=0;$i<@cel_names;$i++)
				{	
					$WRBUFF = "WRBUFF".$i;
					$j = $i+1;
					$k = $j+$len_array;
					print $WRBUFF $array[0]."\t".$array[$j]."\t".sprintf("%.2f",$array1[$j])."\t";
					if($array1[$j] < 0.06)
					{
						print $WRBUFF "Y"."\n";
					}
					else
					{
						print $WRBUFF "N"."\n";
					}		
				}

			}
	
		}
		system("rm -rf $dir/temp/$ascii_dir_out/");
		close(BUFF);
		close(BUFF1);
		for($i=0;$i<@cel_names;$i++)
		{
        		$WRBUFF = "WRBUFF".$i;
			close($WRBUFF);
		}
	}
}

#sprintf("%.0f", rand()*time())
#while($line = <BUFF1>)
#{
 #       chomp $line;
  #      $line =~ s/\n//g;
   #     $line =~ s/\r//g;
    #    @array = split("\t",$line);
     #   if($hash{$array[0]} eq "k")
     #   {
     #           print WRBUFF1 $line."\n";
     #   }

#}




